package vue.panels;

import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import controler.*;

import javax.swing.*;

import vue.Ui;
import vue.components.*;
/**
 * <p>Title: vue.CreerTablePanel </p>
 *
 * <p>
 * 	Description: Cette classe gere l'affichage du panneau de creation de table
 * </p>
 *
 * <p>Copyright: (c) 2008 FSSIM</p>
 *
 * <p>Company: FSSImock</p>
 *
 * @author Antoine Farnault
 * @author Augustin Couette
 * @version 1.0
 */
public class CreerTablePanel extends TPPanel {
   
	private static final long serialVersionUID = 1L;
	public Ui ui;
	
    // Variables declaration
    private TPButton annulerButton;
    private TPLabel capaciteLabel;
    public JSpinner capaciteSpinner;
    private TPButton creerButton;
    private TPLabel nomLabel;
    public TPTextField nomText;
    public JCheckBox passBox;
    public TPPassewordField passText;
    private TPLabel proprietaireLabel;
    public TPTextField proprietaireText;
    public JComboBox themeComboBox;
    private TPLabel themeLabel;
    // End of variables declaration
	
	/** Creates new form CreerTablePanel */
    public CreerTablePanel(Ui ui) {
        initComponents();
        this.ui = ui;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
    	setName("creerTablePanel");
    	setTPTitle("Creer une table");
    	nomLabel = new TPLabel();
        themeLabel = new TPLabel();
        proprietaireLabel = new TPLabel();
        nomText = new TPTextField();
        proprietaireText = new TPTextField();
        passBox = new JCheckBox();
        passText = new TPPassewordField();
        creerButton = new TPButton();
        annulerButton = new TPButton();
        themeComboBox = new JComboBox();
        capaciteLabel = new TPLabel();
        capaciteSpinner = new JSpinner();

        nomLabel.setText("Nom");
        
        capaciteSpinner.setValue(4);
        
        themeLabel.setText("Theme");

        proprietaireLabel.setText("Proprietaire");

        proprietaireText.setEditable(false);

        passBox.setText("Mot de passe");
        passBox.setHorizontalTextPosition(SwingConstants.LEADING);
        passBox.setOpaque(false);
        passText.setEditable(false);

        creerButton.setText("Creer");

        annulerButton.setText("Annuler");
        
        // TODO :: Creer la class de model de themeCombox
        themeComboBox.setModel(new DefaultComboBoxModel(new String[] { "Theme 1", "Theme 2", "Theme 3", "Theme 4" }));
        
        capaciteLabel.setText("Capacite");
        passBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if(passBox.isSelected()){
                	passText.setEditable(true);
                }
                else{
                	passText.setEditable(false);
                }
            }
        });
        
        nomText.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		nomTextKeyPressed(e);
        	}
        });
        
        proprietaireText.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		nomTextKeyPressed(e); // meme action que si l'event est sur nomText
        	}
        });
        
        themeComboBox.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		nomTextKeyPressed(e); // meme action que si l'event est sur nomText
        	}
        });
        
        passText.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		nomTextKeyPressed(e); // meme action que si l'event est sur nomText
        	}
        });
        
        passText.setEditable(false);

        creerButton.setText("Creer");
        
        creerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	creerButtonActionPerformed(evt);
            }
        });
        
        creerButton.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		nomTextKeyPressed(e); // meme action que si l'event est sur nomText
        	}
        });
        
        annulerButton.setText("Annuler");
        annulerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	annulerActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(capaciteLabel)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addComponent(nomLabel)
                                        .addComponent(themeLabel)
                                        .addComponent(passBox))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(themeComboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(nomText)
                                        .addComponent(passText, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                        .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(proprietaireText)
                                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
                                        .addComponent(capaciteSpinner)))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(annulerButton, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(creerButton, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap(50, Short.MAX_VALUE))
                        .addComponent(proprietaireLabel))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(proprietaireLabel)
                    .addComponent(proprietaireText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(nomLabel)
                    .addComponent(nomText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(themeLabel)
                    .addComponent(themeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(capaciteLabel, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
                    .addComponent(capaciteSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(passBox)
                    .addComponent(passText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(annulerButton)
                    .addComponent(creerButton))
                .addContainerGap())
        );
    }
    
    /**
     * Action sur le bouton annuler 
     */
    private void annulerActionPerformed(ActionEvent evt) {
    	Joueur.ui.cp.layout.show(Joueur.ui.cp, "listTablesEtChatPanel");
	}
    
    /**
     * Touche entree sur le nomText
     * @param e
     */
    private void nomTextKeyPressed(KeyEvent e){
    	if(e.getKeyCode() == 10){
    		creerButtonActionPerformed(null);
    	}
    }
    /**
     * Action sur le bouton creer
     * @param evt
     */
	@SuppressWarnings("unchecked")
	private void creerButtonActionPerformed(ActionEvent evt) {
		String pass="";
		String nomTable = nomText.getText();
		
		// la table ne doit pas s'appeler "salle"
		if(nomTable.equalsIgnoreCase("salle")){
			Joueur.ui.statutPanel.setInfo("Une table ne peut s'appeler "+nomTable+" !");
			return;
		}
		// la table ne doit pas s'appeler "selectionnez" (je sais c'est ridicule ;-))
		if(nomTable.equalsIgnoreCase("selectionnez")){
			Joueur.ui.statutPanel.setInfo("Une table ne peut s'appeler "+nomTable+" !");
			return;
		}
		// creation de table doit ne pas avoir un nom qui existe deja
		for (Iterator iterator = Joueur.getTables().iterator(); iterator.hasNext();) {
			Table table = (Table) iterator.next();
			if(table.nom.equals(nomTable)){
				Joueur.ui.statutPanel.setInfo("Ce nom existe deja !");
				return;
			}
		}
		
		boolean privee = false;
		if(passBox.isSelected()){
			privee = true;
			for (int i = 0; i < passText.getPassword().length; i++) {
				pass += passText.getPassword()[i];
			}
		}
		int capacite = (Integer)capaciteSpinner.getValue();
		if(capacite < 2){
			ui.statutPanel.setInfo("Au moins 2 joueurs sont necessaires pour creer une table !");
			return;
		}
		// a la creation il y a 0 joueur
		try {
			Protocol.snd_table(nomTable, (String)themeComboBox.getSelectedItem(),0,capacite,pass,privee);
		} catch (ProtocolException e) {
			e.printStackTrace();
		}
		Joueur.ui.cp.layout.show(Joueur.ui.cp, "listTablesEtChatPanel");
		
	}
	
}
